package com.example.autoclick2;

import android.accessibilityservice.AccessibilityService;
import android.accessibilityservice.GestureDescription;
import android.graphics.Path;
import android.os.Handler;
import android.os.Looper;
import android.view.accessibility.AccessibilityEvent;
import android.widget.Toast;

public class AutoClickService extends AccessibilityService {

    private static AutoClickService instance;
    private boolean isRunning = false;
    private Handler handler;
    private int x = 0, y = 0;
    private int interval = 1000; // 默认点击间隔1秒
    private int clickMode = 0; // 点击模式：0-单击，1-双击，2-向上滑动，3-向下滑动

    // 用于连点的Runnable
    private final Runnable clickRunnable = new Runnable() {
        @Override
        public void run() {
            if (isRunning && x != 0 && y != 0) {
                performClick(x, y);
                handler.postDelayed(this, interval);
            }
        }
    };

    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;
        handler = new Handler(Looper.getMainLooper());
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        stopClick();
        instance = null;
    }

    @Override
    public void onAccessibilityEvent(AccessibilityEvent event) {
        // 不需要处理事件
    }

    @Override
    public void onInterrupt() {
        stopClick();
    }

    // 执行点击操作
    private void performClick(int x, int y) {
        switch (clickMode) {
            case 0: // 单击
                performSingleClick(x, y);
                break;
            case 1: // 双击
                performDoubleClick(x, y);
                break;
            case 2: // 向上滑动
                performSwipeUp(x, y);
                break;
            case 3: // 向下滑动
                performSwipeDown(x, y);
                break;
        }
    }
    
    // 执行单击
    private void performSingleClick(int x, int y) {
        GestureDescription.Builder builder = new GestureDescription.Builder();
        Path path = new Path();
        path.moveTo(x, y);
        
        // 添加点击手势，0表示开始时间，100表示持续时间（毫秒）
        builder.addStroke(new GestureDescription.StrokeDescription(path, 0, 100));
        
        dispatchGesture(builder.build(), null, null);
    }
    
    // 执行双击
    private void performDoubleClick(int x, int y) {
        GestureDescription.Builder builder = new GestureDescription.Builder();
        Path path = new Path();
        path.moveTo(x, y);
        
        // 第一次点击
        builder.addStroke(new GestureDescription.StrokeDescription(path, 0, 100));
        // 第二次点击（间隔150毫秒）
        builder.addStroke(new GestureDescription.StrokeDescription(path, 150, 100));
        
        dispatchGesture(builder.build(), null, null);
    }
    
    // 执行向上滑动
    private void performSwipeUp(int x, int y) {
        GestureDescription.Builder builder = new GestureDescription.Builder();
        Path path = new Path();
        path.moveTo(x, y);
        path.lineTo(x, y - 500); // 向上滑动500像素
        
        builder.addStroke(new GestureDescription.StrokeDescription(path, 0, 400));
        
        dispatchGesture(builder.build(), null, null);
    }
    
    // 执行向下滑动
    private void performSwipeDown(int x, int y) {
        GestureDescription.Builder builder = new GestureDescription.Builder();
        Path path = new Path();
        path.moveTo(x, y);
        path.lineTo(x, y + 500); // 向下滑动500像素
        
        builder.addStroke(new GestureDescription.StrokeDescription(path, 0, 400));
        
        dispatchGesture(builder.build(), null, null);
    }

    // 开始连点
    public void startClick() {
        if (x == 0 || y == 0) {
            Toast.makeText(this, "请先设置点击坐标", Toast.LENGTH_SHORT).show();
            return;
        }
        
        isRunning = true;
        handler.post(clickRunnable);
        Toast.makeText(this, "已开始连点", Toast.LENGTH_SHORT).show();
    }

    // 停止连点
    public void stopClick() {
        isRunning = false;
        handler.removeCallbacks(clickRunnable);
        Toast.makeText(this, "已停止连点", Toast.LENGTH_SHORT).show();
    }

    // 设置点击坐标
    public void setClickPosition(int x, int y) {
        this.x = x;
        this.y = y;
        Toast.makeText(this, "已设置点击坐标: (" + x + ", " + y + ")", Toast.LENGTH_SHORT).show();
    }

    // 设置点击间隔（毫秒）
    public void setInterval(int interval) {
        if (interval < 100) {
            interval = 100; // 设置最小间隔为100毫秒
        }
        this.interval = interval;
        Toast.makeText(this, "已设置点击间隔: " + interval + "毫秒", Toast.LENGTH_SHORT).show();
    }
    
    // 设置点击模式
    public void setClickMode(int mode) {
        this.clickMode = mode;
        String[] modeNames = {"单击", "双击", "向上滑动", "向下滑动"};
        Toast.makeText(this, "已设置点击模式: " + modeNames[mode], Toast.LENGTH_SHORT).show();
    }
    
    // 获取当前点击模式
    public int getClickMode() {
        return clickMode;
    }

    // 获取服务实例
    public static AutoClickService getInstance() {
        return instance;
    }

    // 检查服务是否运行
    public static boolean isServiceRunning() {
        return instance != null;
    }
}